package xdclass_websocket.controller.v5;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import xdclass_websocket.utils.HttpUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * 功能描述：接口服务，调用股票行情接口 *
 * @since 0.0.1
 */
public class StockService {

	public static Map<String, String > getStockInfo(){
		String host = "http://stock.market.alicloudapi.com";
		String path = "/sz-sh-stock-history";
		String method = "GET";
		String appcode = "f8c1e8db12284d519be99ab8c8329955";
		Map<String, String> headers = new HashMap<String, String>();
		//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("begin", "2019-10-06");
		querys.put("code", "600519");
		querys.put("end", "2019-11-05");

	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
			String m = EntityUtils.toString( response.getEntity());

	    	JSONObject obj = JSONObject.parseObject(EntityUtils.toString( response.getEntity()));



	    	if (obj.containsKey("showapi_res_body")) {
    		JSONObject snapshotObj =  obj.getJSONObject("showapi_res_body");

	    		if(snapshotObj.containsKey("list")) {
	    			JSONArray szArray = snapshotObj.getJSONArray("list");
	    		  JSONArray fields = snapshotObj.getJSONObject("snapshot").getJSONArray("fields");
	    			Map<String, String> mapInfo = new HashMap<>();

	    			for(int i=0; i<szArray.size(); i++){
	    				mapInfo.put(fields.getString(i), szArray.getString(i));
	    			}
	    			System.out.println(mapInfo);
	    			return mapInfo;
	    		}

				System.out.println(obj);

	    	}


	    } catch (Exception e) {
	    	e.printStackTrace();
	    }


		return null;

	}




}
